package com.foreknow.view;

import com.foreknow.bean.User;
import com.foreknow.service.UserService;
import com.foreknow.service.UserServiceImpl;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Login extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JPasswordField passwordField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Login frame = new Login();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Login() {
        setTitle("\u767B\u5F55");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JButton btnNewButton = new JButton("\u767B\u5F55");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                //要做的事
                System.out.println("login");
                UserService service = new UserServiceImpl();
                String adminName = textField.getText();
                String password = passwordField.getText();
//                System.out.println(adminName+"-----"+password);
                User user = service.isValidate(adminName, password);
                System.out.println("welcome "+user.getAdminName());
            }
        });
        btnNewButton.setBounds(75, 193, 93, 23);
        contentPane.add(btnNewButton);

        JLabel lblNewLabel = new JLabel("\u7528\u6237\u540D:");
        lblNewLabel.setBounds(124, 98, 42, 29);
        contentPane.add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("\u5BC6\u7801:");
        lblNewLabel_1.setBounds(124, 137, 42, 29);
        contentPane.add(lblNewLabel_1);

        textField = new JTextField();
        textField.setBounds(170, 98, 156, 29);
        contentPane.add(textField);
        textField.setColumns(10);

        passwordField = new JPasswordField();
        passwordField.setBounds(170, 137, 156, 29);
        contentPane.add(passwordField);

        JButton btnNewButton_1 = new JButton("\u53D6\u6D88");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("取消");
            }
        });
        btnNewButton_1.setBounds(309, 193, 93, 23);
        contentPane.add(btnNewButton_1);

        JButton btnNewButton_2 = new JButton("\u6CE8\u518C");
        btnNewButton_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("register");
                UserService service = new UserServiceImpl();
                String adminName = textField.getText();
                String password = passwordField.getText();
                User user = new User();
                user.setAdminName(adminName);
                user.setPassword(password);
                boolean isRight = service.addUser(user);
                System.out.println(isRight);
            }
        });
        btnNewButton_2.setBounds(195, 193, 93, 23);
        contentPane.add(btnNewButton_2);
    }
}
